home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / DirectInput / DIConfig / bidirlookup.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  2.5 KB  |  156 lines

  1. //-----------------------------------------------------------------------------
  2. // File: bidirlookup.h
  3. //
  4. // Desc: This file implements a bi-directional map class template.  It does
  5. //       this by using two CMap classes that each handles the look up in one
  6. //       direction.
  7. //
  8. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  9. //-----------------------------------------------------------------------------
  10.  
  11. #ifndef __BIDIRLOOKUP_H__
  12. #define __BIDIRLOOKUP_H__
  13.  
  14.  
  15. #ifndef NULL
  16. #define NULL 0
  17. #endif
  18.  
  19.  
  20. template <class L, class R>
  21. class bidirlookup
  22. {
  23. private:
  24.     
  25.     CMap<L, const L &, R, const R &> l2r;
  26.     CMap<R, const R &, L, const L &> r2l;
  27.  
  28.     bool addnode(const L &l, const R &r)
  29.     {
  30.         l2r.SetAt(l, r);
  31.         r2l.SetAt(r, l);
  32.         return true;
  33.     }
  34.  
  35. public:
  36.     void clear()
  37.     {
  38.         l2r.RemoveAll();
  39.         r2l.RemoveAll();
  40.     }
  41.  
  42.     bidirlookup() {}
  43.     ~bidirlookup() {clear();}
  44.  
  45.     bool add(const L &l, const R &r)
  46.     {
  47.         L tl;
  48.         R tr;
  49.  
  50.         if (l2r.Lookup(l, tr) || r2l.Lookup(r, tl))
  51.             return false;
  52.         
  53.         return addnode(l, r);
  54.     }
  55.  
  56.     bool getleft(L &l, const R &r)
  57.     {
  58.         return r2l.Lookup(r, l) ? true : false;
  59.     }
  60.  
  61.     bool getright(const L &l, R &r)
  62.     {
  63.         return l2r.Lookup(l, r) ? true : false;
  64.     }
  65. };
  66.  
  67.  
  68. #if 0
  69.  
  70. template <class L, class R>
  71. class bidirlookup
  72. {
  73. private:
  74.     struct node {
  75.         node(const L &a, const R &b) : l(a), r(b), next(NULL) {}
  76.         node *next;
  77.         L l;
  78.         R r;
  79.     } *head;
  80.  
  81.     bool addnode(const L &l, const R &r)
  82.     {
  83.         node *old = head;
  84.         head = new node(l, r);
  85.         if (!head)
  86.             return false;
  87.         head->next = old;
  88.         return true;
  89.     }
  90.  
  91.     node *getleftnode(const L &l)
  92.     {
  93.         for (node *on = head; on; on = on->next)
  94.             if (on->l == l)
  95.                 return on;
  96.         return NULL;
  97.     }
  98.  
  99.     node *getrightnode(const R &r)
  100.     {
  101.         for (node *on = head; on; on = on->next)
  102.             if (on->r == r)
  103.                 return on;
  104.         return NULL;
  105.     }
  106.  
  107. public:
  108.     void clear()
  109.     {
  110.         while (head)
  111.         {
  112.             node *next = head->next;
  113.             delete head;
  114.             head = next;
  115.         }
  116.     }
  117.  
  118.     bidirlookup() : head(NULL) {}
  119.     ~bidirlookup() {clear();}
  120.  
  121.     bool add(const L &l, const R &r)
  122.     {
  123.         if (getleftnode(l) || getrightnode(r))
  124.             return false;
  125.         
  126.         return addnode(l, r);
  127.     }
  128.  
  129.     bool getleft(L &l, const R &r)
  130.     {
  131.         node *n = getrightnode(r);
  132.         if (!n)
  133.             return false;
  134.  
  135.         l = n->l;
  136.  
  137.         return true;
  138.     }
  139.  
  140.     bool getright(const L &l, R &r)
  141.     {
  142.         node *n = getleftnode(l);
  143.         if (!n)
  144.             return false;
  145.  
  146.         r = n->r;
  147.  
  148.         return true;
  149.     }
  150. };
  151.  
  152. #endif
  153.  
  154.  
  155. #endif //__BIDIRLOOKUP_H__
  156.